# 订单信息管理视图文件
from django.shortcuts import render
from django.http import HttpResponse
from myadmin.models import User, Orders, OrderDetail, Payment, Member
from datetime import datetime
from django.core.paginator import Paginator


def index(request, pIndex=1):
    # 浏览订单
    umod = Orders.objects
    # 获取当前店铺id号
    sid = request.session['shopinfo']['id']
    status = request.GET.get('status', None)
    ulist = umod.filter(shop_id=sid, status=status)
    ulist = ulist.order_by("-id")  # 对id排序
    # 执行分页处理
    pIndex = int(pIndex)
    page = Paginator(ulist, 5)  # 以每页5条数据分页
    maxpage = page.num_pages  # 获取最大页数
    # 判断当前页是否越界
    if pIndex > maxpage:
        pIndex = maxpage
    if pIndex < 1:
        pIndex = 1
    list2 = page.page(pIndex)  # 获取当前页
    for vo in list2:
        if vo.user_id == 0:
            vo.nickname = "无"
        else:
            user = User.objects.only("nickname").get(id=vo.user_id)
            vo.nickname = user.nickname
        if vo.member_id == 0:
            vo.membername = "大堂顾客"
        else:
            member = Member.objects.only("mobile").get(id=vo.member_id)
            vo.membername = member.mobile
        p = Payment.objects.only("type", "bank").get(order_id=vo.id)
        vo.type = p.type
        vo.bank = p.bank
    plist = page.page_range  # 获取页码列表
    context = {"orderslist": list2, "plist": plist, "pIndex": pIndex, "maxpage": maxpage, "counter": len(ulist),
               "status": status}
    return render(request, "web/list.html", context)


def insert(request):
    # 执行订单添加
    try:
        # 执行订单信息的添加
        od = Orders()
        od.shop_id = request.session['shopinfo']['id']
        od.member_id = 0
        od.user_id = request.session['webuser']['id']
        od.money = request.session['total_money']
        od.status = 1  # 订单状态:1过行中/2无效/3已完成
        od.payment_status = 2  # 支付状态:1未支付/2已支付/3已退款
        od.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        od.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        od.save()

        # 执行支付信息添加
        op = Payment()
        op.order_id = od.id
        op.member_id = 0
        op.money = request.session['total_money']
        op.type = request.GET.get('ptype', 2)  # 付款方式：1会员付款/2收银收款
        op.bank = request.GET.get('bank', 3)  # 收款银行渠道:1微信/2余额/3现金/4支付宝
        op.status = 2  # 支付状态:1未支付/2已支付/3已退款
        op.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        op.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        op.save()

        # 执行订单详情添加
        cartlist = request.session.get("cartlist", {})  # 获取购物车中的菜品信息
        for item in cartlist.values():
            ov = OrderDetail()
            ov.order_id = od.id
            ov.product_id = item['id']
            ov.product_name = item['name']
            ov.price = item['price']
            ov.quantity = item['num']
            ov.status = 1
            ov.save()
        request.session['cartlist'] = {}

        return HttpResponse("Y")
    except Exception as err:
        print(err)
        return HttpResponse("N")


def detail(request):
    # 加载订单详情
    oid = request.GET.get("oid", 0)
    dlist = OrderDetail.objects.filter(order_id=oid)
    context = {"detaillist": dlist}
    return render(request, "web/detail.html", context)


def status(request):
    # 修改订单状态
    try:
        oid = request.GET.get("oid", 0)
        ob = Orders.objects.get(id=oid)
        ob.status = request.GET['status']
        ob.save()
        return HttpResponse("Y")
    except Exception as err:
        print(err)
        return HttpResponse("N")
